<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    xmlns:ep="urn:ep"
    extension-element-prefixes="str"
    version="1.0">

    <xsl:template name="add_brs_to_multiline_string">
        <xsl:param name="string" />
        <xsl:for-each select="str:tokenize($string, '&#10;')"><xsl:value-of select="."/><xsl:if test="position()!=last()"><br/></xsl:if></xsl:for-each>
    </xsl:template>

    <xsl:template name="component_title_bar">
        <xsl:param name="title"/>
        <xsl:param name="id"/>
        <xsl:param name="error_message"/> 
        <xsl:param name="about"/>
        <xsl:param name="epc_size_state"/>

<!-- to do...
        <script type="text/javascript">
            Text['about_comp_<xsl:value-of select="$id"/>']  = ['About <xsl:value-of select="$title"/>', '<table width="500" cellpadding="1" cellspacing="0" border="0">'+
            <xsl:for-each select="$about/*">
                '<tr>'+
                '<td valign="top" align="left"><b><xsl:value-of select="name()"/></b></td>' +
                '<td valign="top" align="right"><xsl:copy-of select="."/></td>' +
                '</tr>' +
            </xsl:for-each>
            '</table>'];
            Style['about_comp_<xsl:value-of select="$id"/>'] = ["white","#000099","","","",,"black","#e8e8ff","","","",,,,2,"#000099",2,,,,,"",3,,,];
            // ['#FFFFFF','','#336699','#CCCCCC','','','','','','','','sticky','','',200,'',2,2,10,10,51,1,0,'','']
            //  applyCssFilter();
        </script>
-->
        <xsl:if test="string-length($error_message)>0">
            <div class="ep_component_error" style="background-image: url('{$html_href}/static/images/stop8.png')">
                <xsl:call-template name="add_brs_to_multiline_string"><xsl:with-param name="string" select="$error_message" /></xsl:call-template>
            </div>
        </xsl:if>

        <div class="ep_component_title">
            <img style="float: left" src="{$html_href}/static/images/component_title_left.png" />
            <img style="float: right" src="{$html_href}/static/images/component_title_right.png" />
            <div style="background-image: url('{$html_href}/static/images/component_title_bg.png')"><nobr><xsl:value-of select="$title"/></nobr></div>
        </div>

<!-- to do...
    <span><a href="#" onClick="stm(Text['about_comp_{$id}'],Style['about_comp_{$id}']);return false;">
        <img src="{$html_href}/static/images/ep_window_bar_button_about.png" title="About" width="18" height="18" />
        </a>
        &nbsp;
    </span>
-->
    </xsl:template>

    <xsl:template name="subsection_tab_bar">
        <xsl:param name="active_subsec_id" />
        <xsl:comment>subsection tab bar</xsl:comment>
        <!-- draw subsection switch buttons/tabs; only one can be the active tab! -->
        <div class="ep_subsection_tabpane">
            <xsl:for-each select="./subsection">
<!-- to do...
                <xsl:if test="./context_help">
                    <xsl:attribute name="onMouseOver">setCtxtHelp ( "ctxt_<xsl:value-of select="context_help/@id"/>" ); //event.stopPropagation()</xsl:attribute>
                    <xsl:attribute name="onMouseOut">htm()</xsl:attribute>
                </xsl:if>
-->
                <xsl:choose>
                    <xsl:when test="./@id=$active_subsec_id"><!-- this is active subsection tab -->
                        <div id="subsection_tab___c_{ancestor::component/@id}_s_{ancestor::section/@id}_su_{@id}" class="tab_active" style="background-image: url('{$html_href}/static/images/tab_active_bg.png')">
                            <img src="{$html_href}/static/images/tab_active_left.png" />
                            <span class="label"><a href="javascript:ep_tabs_show({ancestor::component/@id},{ancestor::section/@id},{@id});"><xsl:value-of select="./@title"/></a></span>
                            <img src="{$html_href}/static/images/tab_active_right.png" />
                        </div>
                    </xsl:when>
                    <xsl:otherwise>
                        <div id="subsection_tab___c_{ancestor::component/@id}_s_{ancestor::section/@id}_su_{@id}" class="tab_inactive" style="background-image: url('{$html_href}/static/images/tab_inactive_bg.png')">
                            <img src="{$html_href}/static/images/tab_inactive_left.png" />
                            <span class="label"><a href="javascript:ep_tabs_show({ancestor::component/@id},{ancestor::section/@id},{@id});"><xsl:value-of select="./@title"/></a></span>
                            <img src="{$html_href}/static/images/tab_inactive_right.png" />
                        </div>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </div>
<!-- to do...
            <xsl:attribute name="colspan"><xsl:value-of select="count(./subsection)*2+1"/></xsl:attribute>
            <xsl:if test="./subsection[@id=$active_subsec_id]/context_help">
              <xsl:attribute name="onMouseOver">setCtxtHelp ( "ctxt_<xsl:value-of select="subsection[@id=$active_subsec_id]/context_help/@id"/>" ); //event.stopPropagation()</xsl:attribute>
              <xsl:attribute name="onMouseOut">htm()</xsl:attribute>
            </xsl:if>
              <xsl:for-each select="./subsection">               
              <span class="toolInputHelp" id="shs_subsection_{ancestor::section/@id}_{@id}">
                <xsl:attribute name="style"><xsl:if test="@id!=$active_subsec_id">display:none</xsl:if></xsl:attribute>
                <xsl:copy-of select="shorthelp"/>&nbsp;
              </span>
            </xsl:for-each>
-->
    </xsl:template>

 
  <xsl:template name="render_tree_object">
    <xsl:param name="DE"/>

    <table cellspacing="0" cellpadding="0" width="100%">
      <tr>
        <xsl:choose>
          <xsl:when test="name()='user'">    <xsl:call-template name="user"><xsl:with-param name="DE" select="$DE"/></xsl:call-template></xsl:when>
          <xsl:when test="name()='folder'">  <xsl:call-template name="folder"><xsl:with-param name="DE" select="$DE"/></xsl:call-template></xsl:when>
          <xsl:when test="name()='dataset'"> <xsl:call-template name="dataset"><xsl:with-param name="DE" select="$DE"/></xsl:call-template></xsl:when>
          <xsl:when test="name()='action'">  <xsl:call-template name="action"><xsl:with-param name="DE" select="$DE"/></xsl:call-template></xsl:when>
          <xsl:when test="name()='meta'">    <xsl:call-template name="meta"><xsl:with-param name="DE" select="$DE"/></xsl:call-template></xsl:when>
        </xsl:choose>
      </tr>
    </table>
  </xsl:template>

  <xsl:template name="aux">
    <xsl:attribute name="style">font-size: 10px</xsl:attribute>
    <xsl:attribute name="onmouseover">this.style.backgroundColor='#dfdfdf';this.style.cursor='pointer';</xsl:attribute>
    <xsl:attribute name="onmouseout">this.style.backgroundColor='#ffffff'</xsl:attribute>
  </xsl:template>
  
  <xsl:template name="user">
    <xsl:param name="DE"/>

    <td width="100%" align="left">
      <xsl:call-template name="aux"/>
      <span style="padding-left:7px">
        <xsl:value-of select="@first_name"/>&#160;<xsl:value-of select="@last_name"/> (<i><xsl:value-of select="@username"/></i>)
      </span>
    </td>
  </xsl:template>

  <xsl:template name="folder">
    <xsl:param name="selectable"/>

    <td align="left" width="100%">
      <xsl:call-template name="aux"/>
      <xsl:variable name="bold"><!--<xsl:if test="@is_current">bold</xsl:if>--></xsl:variable>

      <span style="padding-left:7px;font-weight:{$bold}">
        <xsl:value-of select="@name"/>
      </span>
    </td>

    <td height="15" align="right"><nobr>
      <xsl:if test="not($DE/@mode='select_only')">
        <a href="javascript:new_folder({@id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/new_folder.png" title="New folder here..."/></a>
        <a href="javascript:delete_folder({@id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/delete_folder_small.png" title="Delete folder"/></a>
      </xsl:if></nobr>
    </td>
    <td valign="middle" width="22">
      <xsl:if test="DE/selectable/folder">
        <input id="chk_folder_{@id}" name="chk_folder_{@id}" style="height:10px; width:10px; font-weight: bold;" type="checkbox"/>
      </xsl:if>
    </td>
  </xsl:template>

  <xsl:template name="dataset">
    <xsl:param name="DE"/>
    <xsl:variable name="selectable">
      <xsl:choose>
        <xsl:when test="$DE/selectable/dataset and $DE/selectable/dataset/@type">
          <xsl:variable name="this_type" select="@type"/>
          <xsl:if test="count(str:tokenize($DE/selectable/dataset/@type,'|')[.=$this_type])">1</xsl:if>
        </xsl:when>
        <xsl:when test="$DE/selectable/dataset and not($DE/selectable/dataset/@type)">1</xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <td align="left" width="100%" onclick="toggle(chk_dataset_{@dataset_id});">
      <xsl:call-template name="aux"/>
      <xsl:variable name="bold"><!--<xsl:if test="@is_current">bold</xsl:if>--></xsl:variable>

      <span style="padding-left:7px;font-weight:{$bold}">
        <xsl:value-of select="@dataset_name"/>
      </span>
    </td>

    <td height="15" align="right">
        <nobr>
            <a href="{@data}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/data.gif" title="View data matrix ({@num_rows}x{@num_columns})..."/></a>
            <a href="{@rows}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/rows.gif" title="View rows ({@num_rows})..."/></a>
            <a href="{@cols}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/cols.gif" title="View columns ({@num_columns})..."/></a>
            <xsl:if test="not($DE/@mode='select_only')">
                <!--        <a href="javascript:saveas_action({@dataset_id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/saveas.gif" title="Save As..."/></a>-->
                <a href="javascript:delete_dataset({@dataset_id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/delete_dataset_small.png" title="Delete dataset"/></a>
            </xsl:if>
        </nobr>
    </td>
    <td valign="middle" width="22">
      <xsl:if test="$selectable=1">
        <input id="chk_dataset_{@dataset_id}" name="chk_dataset_{@dataset_id}" style="height:10px; width:10px; font-weight: bold;" type="checkbox"/>
      </xsl:if>
    </td>
  </xsl:template>

  <xsl:template name="action">
    <xsl:param name="DE"/>
    <xsl:variable name="selectable">
      <xsl:choose>
        <xsl:when test="$DE/selectable/dataset and $DE/selectable/dataset/@type">
          <xsl:variable name="this_type" select="ancestor::dataset/@type"/>
          <xsl:if test="count(str:tokenize($DE/selectable/dataset/@type,'|')[.=$this_type])">1</xsl:if>
        </xsl:when>
        <xsl:when test="$DE/selectable/action">1</xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <td align="left" width="100%" onclick="toggle(chk_action_{@action_id});">
      <xsl:call-template name="aux"/>
      <xsl:variable name="bold"><!--<xsl:if test="@is_current">bold</xsl:if>--></xsl:variable>

      <span style="padding-left:7px;font-weight:{$bold}">
        <xsl:variable name="TTT">
          <xsl:for-each select="meta[@label!=''][@type!='xml/table'][not(contains(@type,'image'))][@type!='chime'][@type!='text/plain']"><xsl:value-of select="@label"/>: <xsl:copy-of select="text()"/><xsl:if test="position()!=last()">; </xsl:if></xsl:for-each>
        </xsl:variable>

        <xsl:value-of select="@component_name"/><span style="color:#808080;font-weight:normal"> - <xsl:value-of select="substring-before(@date,'.')"/>: <xsl:value-of select="$TTT"/></span>
      </span>
    </td>

    <td height="15" align="right"><nobr>
      <xsl:if test="not($DE/@mode='select_only')">
        <a href="{$cgi_href}/display_action_meta.pl?action_id={@action_id}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/goto.png" title="Display..."/></a>
        <a href="javascript:saveas_action({@id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/saveas.gif" title="Save As..."/></a>
        <a href="javascript:delete_action({@action_id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/delete_dataset_small.png" title="Delete action"/></a>
      </xsl:if></nobr>
    </td>
    <td valign="middle" width="22">
      <xsl:if test="$selectable=1">
        <input id="chk_action_{@action_id}" name="chk_action_{@action_id}" style="height:10px; width:10px; font-weight: bold;" type="checkbox"/>
      </xsl:if>
    </td>
  </xsl:template>

  <xsl:template name="meta">
    <xsl:param name="DE"/>
    <xsl:variable name="selectable">
      <xsl:choose>
        <xsl:when test="$DE/selectable/dataset and $DE/selectable/dataset/@type">
          <xsl:variable name="this_type" select="ancestor::dataset/@type"/>
          <xsl:if test="count(str:tokenize($DE/selectable/dataset/@type,'|')[.=$this_type])">1</xsl:if>
        </xsl:when>
        <xsl:when test="$DE/selectable/action">1</xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <td align="left" width="100%" onclick="toggle(chk_meta_{@id});">
      <xsl:call-template name="aux"/>

      <span style="padding-left:7px;font-style:italic">
        <xsl:value-of select="@label"/>
      </span>
    </td>

    <td height="15" align="right"><nobr>
      <xsl:if test="not($DE/@mode='select_only')">
        <a href="{$cgi_href}/display_action_meta.pl?action_id={../@action_id}&amp;meta={@id}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/goto.png" title="Display..."/></a>
        <a href="javascript:saveas_action({@id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/saveas.gif" title="Save As..."/></a>
        <a href="javascript:alert('Will delete the parent action of this metadata item!');delete_action({../@action_id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/delete_dataset_small.png" title="Delete action"/></a>
      </xsl:if>
                                  </nobr>
    </td>
    <td valign="middle" width="22">
      <xsl:if test="$selectable=1">
        <input id="chk_meta_{@id}" name="chk_meta_{@id}" style="height:10px; width:10px; font-weight: bold;" type="checkbox"/>
      </xsl:if>
    </td>
  </xsl:template>

  <xsl:template name="render_folder_tree_DM">
    <xsl:param name="folder"/>
    <xsl:param name="current_folder_id_input_name"/>
    <xsl:param name="current_root_dataset_id_input_name"/>

    <xsl:variable name="gid" select="generate-id($folder)"/>

    <script type="text/javascript">
        arrFolders['<xsl:value-of select="$gid"/>'] = new Object();
    </script>
    <div id="entity_{$gid}" open="true">
      <table border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><nobr>
            <xsl:call-template name="hierarchy">
              <xsl:with-param name="this" select="$folder"/>
            </xsl:call-template>
              </nobr></td>
          <td width="100%" valign="top">
            <div id="folder_ac_{$gid}"
              onmouseover="this.style.backgroundColor='white';this.style.cursor='pointer';"
              onmouseout="this.style.backgroundColor='#eeeeee'"
              onclick="change_current_folder('{$folder/@id}')">
              <xsl:attribute name="style">
                <xsl:if test="$folder/@is_current=1">font-weight: bold;</xsl:if>
              </xsl:attribute>
              <xsl:value-of select="$folder/@name"/>
            </div>
          </td>
        </tr>
      </table>

      <xsl:for-each select="$folder/folder">
        <xsl:call-template name="render_folder_tree_DM">
          <xsl:with-param name="folder" select="."/>
          <xsl:with-param name="current_folder_id_input_name" select="$current_folder_id_input_name"/>
          <xsl:with-param name="current_root_dataset_id_input_name" select="$current_root_dataset_id_input_name"/>
        </xsl:call-template>
      </xsl:for-each>
    </div>
  </xsl:template>

  <!-- ************* analysis history display ************ -->
  <xsl:template name="render_analysis_history">
    <xsl:param name="analysis_history_tree"/>
    <xsl:param name="current_action_id_input_name"/>
    <xsl:param name="selectable_actions" select="0"/>
    <xsl:param name="select_actions_by_epc_id"/>
    <xsl:param name="view_current_only"/>

    <input type="hidden" name="current_action_id" value="{$analysis_history_tree//action[@is_current=1]/@id}"/>
    <input type="hidden" name="current_cluster_selection"/>
    <input type="hidden" name="current_metadata_selection"/>
    <input type="hidden" id="analysis_history_tree_visible" name="analysis_history_tree_visible" value="{$query/@analysis_history_tree_visible}"/>

    <td>
      <div id="analysis_history_tree">
        <xsl:attribute name="style">
          <xsl:choose>
            <xsl:when test="$query/@analysis_history_tree_visible=0">display:none</xsl:when>
            <xsl:when test="not(@analysis_history_tree_visible)">display:none</xsl:when>
          </xsl:choose>
        </xsl:attribute>
        <span class="toolSectionTitle">History</span>
        <xsl:if test="$analysis_history_tree/analysis_history/action">
          <script type="text/javascript">
            var arrSelectedActions = new Array ( <xsl:value-of select="$selectable_actions"/> );
            var selected_actions = 0;
            var selectable_actions = <xsl:value-of select="$selectable_actions"/>;
          </script>
          <xsl:variable name="sel_act_input_name">selected_actions___c_<xsl:value-of select="ancestor::component/@id"/>_s_<xsl:value-of select="ancestor::section/@id"/></xsl:variable>
          <input type="hidden" name="{$sel_act_input_name}" value=""/>
          <xsl:for-each select="$analysis_history_tree/analysis_history/action">
            <xsl:call-template name="render_analysis_history_tree">
              <xsl:with-param name="action" select="."/>
              <xsl:with-param name="selectable_actions" select="$selectable_actions"/>
              <xsl:with-param name="select_actions_by_epc_id" select="$select_actions_by_epc_id"/>
              <xsl:with-param name="current_action_id_input_name" select="$current_action_id_input_name"/>
              <xsl:with-param name="sel_act_input_name" select="$sel_act_input_name"/>
              <xsl:with-param name="view_current_only" select="$view_current_only"/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:if>
        <script type="text/javascript">
          onLoadHooks [ onLoadHooks.length ] = new Function ( 
            "update_action_metadata ( <xsl:value-of select="$analysis_history_tree/analysis_history//action[@is_current=1]/@id"/>);"
          );
        </script>
      </div>
    </td>
  </xsl:template>

  <!-- ************* analysis history tree display ************ -->
  <xsl:template name="render_analysis_history_tree">
    <xsl:param name="action"/>
    <xsl:param name="current_action_id_input_name"/>
    <xsl:param name="view_current_only"/>
    <xsl:param name="selectable_actions" select="0"/>
    <xsl:param name="select_actions_by_epc_id"/>
    <xsl:param name="sel_act_input_name"/>

    <script type="text/javascript">
        arrActions[<xsl:value-of select="$action/@id"/>] = new Object ();
        <xsl:for-each select="$action/@*">arrActions[<xsl:value-of select="$action/@id"/>].<xsl:value-of select="local-name(.)"/> = "<xsl:value-of select="."/>";</xsl:for-each>
    </script>

    <div id="entity_{generate-id($action)}" open="true">
      <table border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><nobr>
            <xsl:call-template name="hierarchy">
              <xsl:with-param name="this" select="$action"/>
            </xsl:call-template>
              </nobr></td>
          <td width="100%">
            <div id="analysis_history_ac_{$action/@id}" onmouseover="this.style.backgroundColor='white';this.style.cursor='pointer';" onmouseout="this.style.backgroundColor='#eeeeee'">
              <xsl:if test="not($view_current_only=1)">
                <xsl:attribute name="onclick">
                  change_current_action('<xsl:value-of select="$action/@id"/>')
                </xsl:attribute>
              </xsl:if>
              <xsl:attribute name="style">
                padding-left: 7px;
                font-family: Verdana;
                font-size: 11px;
                font-color: black;
                <xsl:if test="$action/@is_current=1">
                  font-weight: bold;
                </xsl:if>
              </xsl:attribute>
              
              <xsl:value-of select="$action/@epc_name"/>
              <xsl:variable name="TTT">
                <xsl:for-each select="$action/meta[@label!=''][@type!='xml/table'][not(contains(@type,'image'))][@type!='chime'][@type!='text/plain']"><xsl:value-of select="@label"/>: <xsl:copy-of select="text()"/><xsl:if test="position()!=last()">; </xsl:if></xsl:for-each>
              </xsl:variable>
              <span style="color:#808080;font-weight:normal"> - <xsl:value-of select="substring-before($action/@date,'.')"/>: <xsl:value-of select="$TTT"/></span>
            </div>
          </td>
          <td width="22" height="15" align="center">
            <a onclick="javascript:show_notes({$action/@id})" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/notes.gif" title="View and edit notes..."/></a>
          </td>
          <td width="22" height="15" align="center">
            <a href="{$action/@data}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/data.gif" title="View data matrix ({$action/@num_rows}x{$action/@num_columns})..."/></a>
          </td>
          <td width="22" height="15" align="center">
            <a href="{$action/@rows}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/rows.gif" title="View rows ({$action/@num_rows})..."/></a>
          </td>
          <td width="22" height="15" align="center">
            <a href="{$action/@cols}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/cols.gif" title="View columns ({$action/@num_columns})..."/></a>
          </td>
          <!--
          <td width="22" height="15" align="center">
            <a href="javascript:saveas_action({$action/@id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/saveas.gif" title="Save As..."/></a>
          </td>
          <td width="22" height="15" align="center">
            <a href="javascript:delete_action({$action/@id})"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/delete.gif" title="Delete"/></a>
          </td>
          -->
          <td width="22" height="15" align="center">
            <a href="{$cgi_href}/display_action_meta.pl?action_id={$action/@id}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/goto.png" title="Display..."/></a>
          </td>
          <xsl:if test="$selectable_actions>0">
            <td valign="middle">
              <input id="chk_ac_{$action/@id}" name="chk_ac_{$action/@id}" onclick="select_action({$action/@id}, this, {$sel_act_input_name})" style="height:10px; width:10px; font-weight: bold;" type="checkbox">
	        <xsl:if test="count(str:tokenize($select_actions_by_epc_id,'|')[.=$action/@epc_id])=0">
                  <xsl:attribute name="disabled"/>
                </xsl:if>
              </input>
              <script type="text/javascript">
                  document.mainfrm.chk_ac_<xsl:value-of select="$action/@id"/>.checked=false;
              </script>
            </td>
          </xsl:if>
        </tr>
      </table>
      <div>
        <table border="0" cellspacing="0" cellpadding="0">
          <xsl:for-each select="$action/cluster">
            <tr>
              <td>
                <xsl:call-template name="hierarchy">
                  <xsl:with-param name="this" select="."/>
                </xsl:call-template>
              </td>
              <td width="100%">
                <div id="analysis_history_ac_{$action/@id}_{@direction}_cluster_{@left}:{@right}"
                  onmouseover="this.style.backgroundColor='white';this.style.cursor='pointer';"
                  onmouseout="this.style.backgroundColor='#eeeeee'">
                  <xsl:attribute name="style">
                    padding-left: 7px;
                    font-family: Verdana;
                    font-size: 11px;
                    font-color: black;
                  </xsl:attribute>
                  <xsl:if test="not($view_current_only=1)">
                    <xsl:attribute name="onclick">change_current_cluster_selection('<xsl:value-of select="$action/@id"/>','<xsl:value-of select="@direction"/>','<xsl:value-of select="@left"/>','<xsl:value-of select="@right"/>')</xsl:attribute>
                  </xsl:if>
                  <xsl:if test="@direction='row'">Rows subtree: [<xsl:value-of select="@name"/>]</xsl:if>
                  <xsl:if test="@direction='col'">Columns subtree: [<xsl:value-of select="@name"/>]</xsl:if>
                </div>
              </td>
              <td width="22" height="15" align="center">
                <a href="{$cgi_href}/ep_get_saved_clusters.pl?cluster_id={@id}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/cluster.png" title="Get cluster..."/></a>
              </td>
            </tr>
          </xsl:for-each>
          <xsl:for-each select="$action/meta[@label!=''][@type='xml/table']">
            <tr>
              <td><nobr>
                <xsl:call-template name="hierarchy">
                  <xsl:with-param name="this" select="."/>
                </xsl:call-template>
                  </nobr></td>
              <td width="100%">
                <div id="analysis_history_ac_{$action/@id}_metadata_{@id}"
                  onmouseover="this.style.backgroundColor='white';this.style.cursor='pointer';"
                  onmouseout="this.style.backgroundColor='#eeeeee'">
                  <xsl:attribute name="style">
                    padding-left: 7px;
                    font-family: Verdana;
                    font-size: 10px;
                    font-color: black;
                    font-style: italic;
                  </xsl:attribute>
                  <xsl:if test="not($view_current_only=1)">
                    <xsl:attribute name="onclick">change_current_metadata_selection(<xsl:value-of select="$action/@id"/>,<xsl:value-of select="@id"/>)</xsl:attribute>
                  </xsl:if>
                  <xsl:value-of select="@label"/>
                </div>
              </td>
              <td width="22" height="15" align="center">
                <a href="{$cgi_href}/display_action_meta.pl?action_id={$action/@id}&amp;meta={@id}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/data.gif" title="View table..."/></a>
              </td>
              <td width="22" height="15" align="center">
                <a href="{text()}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/raw_data.png" title="Raw data... ({@type})"/></a>
              </td>
            </tr>
          </xsl:for-each>
          <xsl:for-each select="$action/meta[@label!=''][@type='text/plain']">
            <tr>
              <td><nobr>
                <xsl:call-template name="hierarchy">
                  <xsl:with-param name="this" select="."/>
                </xsl:call-template>
                  </nobr></td>
              <td width="100%">
                <div id="analysis_history_ac_{$action/@id}_metadata_{@id}"
                  onmouseover="this.style.backgroundColor='white';"
                  onmouseout="this.style.backgroundColor='#eeeeee'">
                  <xsl:attribute name="style">
                    padding-left: 7px;
                    font-family: Verdana;
                    font-size: 10px;
                    font-color: black;
                    font-style: italic;
                  </xsl:attribute>
                  <xsl:value-of select="@label"/>
                </div>
              </td>
              <td/>
              <td width="22" height="15" align="center">
                <a href="{text()}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/raw_data.png" title="Raw data... ({@type})"/></a>
              </td>
            </tr>
          </xsl:for-each>
          <xsl:for-each select="$action/meta[starts-with(@name,'clustering_')][@type='text/plain']">
            <tr>
              <td><nobr>
                <xsl:call-template name="hierarchy">
                  <xsl:with-param name="this" select="."/>
                </xsl:call-template>
                  </nobr></td>
              <td width="100%">
                <div id="analysis_history_ac_{$action/@id}_metadata_{@id}"
                  onmouseover="this.style.backgroundColor='white';"
                  onmouseout="this.style.backgroundColor='#eeeeee'">
                  <xsl:attribute name="style">
                    padding-left: 7px;
                    font-family: Verdana;
                    font-size: 10px;
                    font-color: black;
                    font-style: italic;
                  </xsl:attribute>
                  <xsl:value-of select="@label"/>
                </div>
              </td>
              <td/>
              <td width="22" height="15" align="center">
                <a href="{text()}" target="_blank"><img border="0" height="15" src="{$html_href}/static/images/gui_icons/cluster.png" title="@label..."/></a>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </div>
      <xsl:for-each select="$action/action">
        <xsl:call-template name="render_analysis_history_tree">
          <xsl:with-param name="action" select="."/>
          <xsl:with-param name="selectable_actions" select="$selectable_actions"/>
          <xsl:with-param name="select_actions_by_epc_id" select="$select_actions_by_epc_id"/>
          <xsl:with-param name="current_action_id_input_name" select="$current_action_id_input_name"/>
          <xsl:with-param name="view_current_only" select="$view_current_only"/>
          <xsl:with-param name="sel_act_input_name" select="$sel_act_input_name"/>
        </xsl:call-template>
      </xsl:for-each>
    </div>
  </xsl:template>
  
  <!-- analysis history tree hierarchy lines display -->
  <xsl:template name="hierarchy">
    <xsl:param name="this"/>
    <xsl:for-each select="$this/ancestor::*">
      <xsl:choose>
        <xsl:when test="following-sibling::node()">
          <img src="{$html_href}/static/images/gui_tree/I.png"/>
        </xsl:when>
        <xsl:otherwise>
          <img src="{$html_href}/static/images/gui_tree/blank.png"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <!-- funny code that tests if element has meta children, and if it does, whether there are any tables (or other meta that can be viewed as a child in there).
         If yes, then we'll display them, if not - then act like no children found -->
    <xsl:variable name="meta_children">
      <xsl:choose>
        <xsl:when test="count($this/meta)=0">1</xsl:when>
        <xsl:when test="$this/meta/@type='xml/table'">1</xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="count($this/*)>0 and 0=1">
        <xsl:choose>
          <xsl:when test="$this/following-sibling::node()">
            <img onclick="clickOnEntity('{generate-id($this)}');" onselectstart="return false" ondragstart="return false" onmouseover="this.style.cursor='pointer'">
              <xsl:choose>
                <xsl:when test="name($this)='folder'">
                  <xsl:attribute name="src"><xsl:value-of select="$html_href"/>/static/images/gui_icons/folder_open.png</xsl:attribute>
                  <xsl:attribute name="_open"><xsl:value-of select="$html_href"/>/static/images/gui_icons/folder_open.png</xsl:attribute>
                  <xsl:attribute name="_closed"><xsl:value-of select="$html_href"/>/static/images/gui_icons/folder_closed.png</xsl:attribute>
                  <xsl:attribute name="title">Folder</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="src"><xsl:value-of select="$html_href"/>/static/images/gui_tree/Tminus.png</xsl:attribute>
                  <xsl:attribute name="_open"><xsl:value-of select="$html_href"/>/static/images/gui_tree/Tminus.png</xsl:attribute>
                  <xsl:attribute name="_closed"><xsl:value-of select="$html_href"/>/static/images/gui_tree/Tplus.png</xsl:attribute>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:attribute name="ID">stateImage<xsl:value-of select="generate-id($this)"/></xsl:attribute>
            </img>
          </xsl:when>
          <xsl:otherwise>
            <img onclick="clickOnEntity('{generate-id($this)}');" onselectstart="return false" ondragstart="return false" onmouseover="this.style.cursor='pointer'">
              <xsl:choose>
                <xsl:when test="name($this)='folder'">
                  <xsl:attribute name="src"><xsl:value-of select="$html_href"/>/static/images/gui_icons/folder_open.png</xsl:attribute>
                  <xsl:attribute name="_open"><xsl:value-of select="$html_href"/>/static/images/gui_icons/folder_open.png</xsl:attribute>
                  <xsl:attribute name="_closed"><xsl:value-of select="$html_href"/>/static/images/gui_icons/folder_closed.png</xsl:attribute>
                  <xsl:attribute name="title">Folder</xsl:attribute>
                </xsl:when>
                <xsl:when test="name($this)='user'">
                  <xsl:attribute name="src"><xsl:value-of select="$html_href"/>/static/images/gui_icons/user_open.png</xsl:attribute>
                  <xsl:attribute name="_open"><xsl:value-of select="$html_href"/>/static/images/gui_icons/user_open.png</xsl:attribute>
                  <xsl:attribute name="_closed"><xsl:value-of select="$html_href"/>/static/images/gui_icons/user_closed.png</xsl:attribute>
                  <xsl:attribute name="title">Folder</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="src"><xsl:value-of select="$html_href"/>/static/images/gui_tree/Lminus.png</xsl:attribute>
                  <xsl:attribute name="_open"><xsl:value-of select="$html_href"/>/static/images/gui_tree/Lminus.png</xsl:attribute>
                  <xsl:attribute name="_closed"><xsl:value-of select="$html_href"/>/static/images/gui_tree/Lplus.png</xsl:attribute>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:attribute name="ID">stateImage<xsl:value-of select="generate-id($this)"/></xsl:attribute>
            </img>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$this/following-sibling::node()">
            <xsl:choose>
              <xsl:when test="name($this)='folder'">
                <img src="{$html_href}/static/images/gui_icons/folder_closed.png" title="Folder (empty)"/>
              </xsl:when>
              <xsl:otherwise>
                <img src="{$html_href}/static/images/gui_tree/T.png"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="name($this)='folder'">
                <img src="{$html_href}/static/images/gui_icons/folder_closed.png" title="Folder (empty)"/>
              </xsl:when>
              <xsl:otherwise>
                <img src="{$html_href}/static/images/gui_tree/L.png"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:if test="name($this)='dataset'"><img src="{$html_href}/static/images/gui_icons/dataset_small.png" title="Dataset"/></xsl:if>
    <xsl:if test="name($this)='action'"><img src="{$html_href}/static/images/gui_icons/action_small.png" title="Action"/></xsl:if>

  </xsl:template>

</xsl:stylesheet>
